package com.xiaoyu.commonlib.ui.base;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xiaoyu.commonlib.R;
import com.xiaoyu.commonlib.utils.StatusBarUtil;
import com.xiaoyu.commonlib.utils.UIUtils;

/**
 * @date 创建时间 2018/9/29
 * @author jx
 * @Description
 */

public abstract class BaseActivityWithTitle extends BaseActivity implements View.OnClickListener {


    private ImageView mBack; //返回键
    private TextView mTitleMiddleTxt;//中间标题
    private LinearLayout mLinearRight;//右侧内容添加
    private RelativeLayout relativeLayout_title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_with_title);
        StatusBarUtil.StatusBarLightMode(this);
        setOccupyViewLinearGradientLogin();//设置状态栏渐变色
        initView();
    }

    private void initView() {
        mBack =  findViewById(R.id.title_return);
        mTitleMiddleTxt =  findViewById(R.id.title_middle_txt);
        relativeLayout_title = findViewById(R.id.relativeLayout_title);
        mLinearRight =  findViewById(R.id.activity_base_with_title_right);

        mBack.setOnClickListener(this);

        loadContentView((LinearLayout) findViewById(R.id.base_common_activity_title));

    }

    protected void setTitleBackgroudColor(int color) {
        relativeLayout_title.setBackgroundColor(color);
    }

    /**
     * 加载界面并放入parent中
     *
     * @param parent 父容器
     */
    protected abstract void loadContentView(ViewGroup parent);

    /**
     * 中间文字的设置
     *
     * @param resId
     */
    protected void setTitleText(int resId) {
        mTitleMiddleTxt.setText(resId);
    }

    protected void setTitleText(String title) {
        mTitleMiddleTxt.setText(title);
    }

    protected void setTitleTextColor(int color) {
        mTitleMiddleTxt.setTextColor(color);
    }


    /**
     * 隐藏返回图标
     */
    protected void hideBackImg() {
        mBack.setVisibility(View.GONE);
    }

    protected void setBackImg(int img) {
        mBack.setImageResource(img);
    }

    @Override
    public void onClick(View v) {
        if (v == mBack) {
            onBackIconPressed();
        }
    }

    /**
     * 返回图标点击回调
     */
    protected void onBackIconPressed() {
        onBackPressed();
    }


    /**
     * 添加右边图标
     *
     * @param child 图标view
     * @param id    view的id
     */
    protected void addRightView(View child, int id) {
        child.setId(id);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = mLinearRight.getChildCount() == 0 ? 0 : UIUtils.dip2px(18);
        mLinearRight.addView(child, lp);
    }

}